package com.xianbeiying.app.ckgl.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.MyMessageListdapter;
import com.xianbeiying.app.ckgl.bean.MessageBean;
import com.xianbeiying.app.ckgl.bean.MyMessageDeleteBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.DeleteModel;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.popwindow.CenterPopWindow;

/***
 * 我的消息
 */
public class MyMessageActivity extends BaseActivity implements HttpCallBackUtil, View.OnClickListener {
    private ImageView iv_title_left;
    private TextView tv_title;
    private MyMessageListdapter myMessageListdapter;
    private ListView message_list;
    private GetModel getModel;
    private DeleteModel deleteModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);
        initView();
    }

    private void initView() {
        MainActivity.fragmentid = 3;
        getModel = new GetModel(this);
        getModel.addResponseListener(this);
        deleteModel = new DeleteModel(this);
        deleteModel.addResponseListener(this);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        message_list = (ListView) findViewById(R.id.message_list);
        tv_title.setText("我的消息");
        initData();
        initEvent();
    }

    private void initData() {
        getModel.GETMYMESSAGE("message", true);
    }

    private CenterPopWindow centerPopWindow;
    private int selectId;

    private void initEvent() {
        iv_title_left.setOnClickListener(this);
        message_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            }
        });
        message_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //进行删除
                centerPopWindow = new CenterPopWindow(MyMessageActivity.this, R.layout.pop_window_delete);
                centerPopWindow.setText(R.id.tv_title, "确定删除");
                centerPopWindow.setTextOnClickListener(R.id.tv_delete, MyMessageActivity.this);
                centerPopWindow.setTextOnClickListener(R.id.tv_cancel, MyMessageActivity.this);
                centerPopWindow.setTextOnClickListener(R.id.ly_exit, MyMessageActivity.this);
                selectId = position;
                return true;
            }
        });
    }

    private MessageBean messageBean;
    private MyMessageDeleteBean myMessageDeleteBean;

    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals("我的消息")) {
            if (content != null) {
                messageBean = JSON.parseObject(new String((byte[]) content), MessageBean.class);
                if (messageBean != null && messageBean.getMESSAGE().size() != 0) {
                    myMessageListdapter = new MyMessageListdapter(this, messageBean);
                    message_list.setAdapter(myMessageListdapter);
                }else{
                    message_list.setAdapter(null);
                }
            }
        }
        if (url.equals("删除消息")) {
            myMessageDeleteBean = JSON.parseObject(new String((byte[]) content), MyMessageDeleteBean.class);
            if (myMessageDeleteBean.isR()) {
                ToastUtil.showInfo(MyMessageActivity.this, "删除成功");
                centerPopWindow.dismiss();
                getModel.GETMYMESSAGE("message", true);
            } else {
                centerPopWindow.dismiss();
                ToastUtil.showInfo(MyMessageActivity.this, myMessageDeleteBean.getERR_MSG());
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            //删除
            case R.id.tv_delete:
                deleteModel.DeleteMessage("DELETE_MESSAGE", messageBean.getMESSAGE().get(selectId).getSerial(), true);
                break;
            //取消
            case R.id.tv_cancel:
            case R.id.ly_exit:
                centerPopWindow.dismiss();
                break;
        }
    }
}
